package org.code.mallspringboot.controller;

import org.code.mallspringboot.dto.PmsUpdateDto;
import org.code.mallspringboot.pojo.PmsProductCategory;
import org.code.mallspringboot.service.PmsProductCategoryAttributeRelationService;
import org.code.mallspringboot.service.PmsProductCategoryService;
import org.code.mallspringboot.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Love
 *  产品种类
 */
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {


    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    @Autowired
    private PmsProductCategoryAttributeRelationService pmsProductCategoryAttributeRelationService;

    @GetMapping("/list/{parentId}")
    public CommonResult list(@PathVariable int parentId,
                             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {

        List<PmsProductCategory> list = pmsProductCategoryService.selectPage(parentId, pageNum, pageSize);
        int total = pmsProductCategoryService.total(parentId);
        Map map = new HashMap();
        map.put("list", list);
        map.put("total", total);
        return CommonResult.success(map);
    }

    @GetMapping("/delete/{id}")
    public CommonResult delete(@PathVariable int id) {
        int i = pmsProductCategoryService.delete(id);
        if (i > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed("删除失败");
    }

    //提交编辑
    @PostMapping("/update/{id}")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult updateProductCate(@PathVariable int id, @RequestBody PmsUpdateDto pmsUpdateDto) {

        try {
            pmsUpdateDto.setId(id);

            if (pmsProductCategoryService.update(pmsUpdateDto) > 0) {
                if (pmsProductCategoryAttributeRelationService.delete(id) > 0) {
                    System.out.println("删除成功");
                    for (Integer i : pmsUpdateDto.getProductAttributeIdList()) {
                        pmsProductCategoryAttributeRelationService.addForeach(id, i);
                    }
                    return CommonResult.success(null);
                }
            }
        } catch (Exception e) {
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e);
        }
        return CommonResult.failed("修改失败");
    }

    //编辑回显
    @GetMapping("/{id}")
    public CommonResult update(@PathVariable int id) {

        return CommonResult.success(pmsProductCategoryService.selectUpdate(id));
    }

    //添加
    @PostMapping("/create")
    public CommonResult create(@RequestBody PmsUpdateDto pmsUpdateDto){
        if (pmsUpdateDto.getParentId() !=0){
            pmsUpdateDto.setLevel(1);
        }
        if (pmsProductCategoryService.add(pmsUpdateDto) > 0){
            for (Integer i : pmsUpdateDto.getProductAttributeIdList()) {
                pmsProductCategoryAttributeRelationService.addForeach(pmsUpdateDto.getId(),i);
            }
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    //商品列表-商品分类
    @GetMapping("/list/withChildren")
    public CommonResult withChildren(){
        List<PmsProductCategory> level1 = pmsProductCategoryService.select_withChildren(0);
        for (PmsProductCategory level2 : level1) {
            level2.setChildren(pmsProductCategoryService.select_withChildren(level2.getId()));
        }
        if (level1.size() > 0){
            return CommonResult.success(level1);
        }
        return CommonResult.failed("获取商品分类失败!");
    }
}
